$(function () {
	let form = layui.form;
	form.verify({
		nickname: value => {
			if (!new RegExp('^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$').test(value)) {
				return '用户名不能有特殊字符';
			}
			if (/(^\_)|(\__)|(\_+$)/.test(value)) {
				return '用户名首尾不能出现下划线\'_\'';
			}
			if (value.length > 6) {
				return '昵称长度必须在 1 ~ 6 个字符之间! '
			}
		}
	});

	// let id;
	// let nickname = $('.user-update [name="nickname"]');
	// let email = $('.user-update [name="email"]');
	const initUserInfo = () => {
		$.ajax({
			method: 'get',
			url: '/my/userinfo',
			success: res => {
				if (res.status !== 0) return layer.msg('获取用户信息失败！');
				// id = res.data.id;
				// $('.user-update [name="username"]').val(res.data.username);
				// nickname.val(res.data.nickname);
				// email.val(res.data.email);
				form.val('formUserInfo', res.data);
			}
		});
	}
	initUserInfo();

	$('.user-update [type="reset"]').on('click', function (e) {
		e.preventDefault()
		initUserInfo();
	});

	$('.user-update').on('submit', function (e) {
		e.preventDefault();
		$.ajax({
			url: '/my/userinfo',
			method: 'post',
			data: $(this).serialize(),
			// data: { id: id, nickname: nickname.val(), email: email.val() },
			success: res => {
				if (res.status != 0) return layer.msg(res.message);
				layer.msg(res.message);
				window.parent.getUserInfo();
			}
		});
	});
});